import { defineStore } from 'pinia';
import { patchStockItem, updateChildStockCodeSequence } from 'src/services/stockCodeService';
import {
  assignChildStock,
  createStockItem,
  getChildStockItems,
  removeStockItemChild,
  updateStockItem,
} from 'src/services/stockCodeService';
import { ItemForm, StockItem } from 'src/services/type';
export const useChildMapStore = defineStore('useChildMapStore', {
  state: () => ({
    itemList: itemList,
  }),
  getters: {},
  actions: {
    /* update state of itemList sequence */
    updateSequence(event: any) {
      const temp = this.itemList[event.oldIndex];
      this.itemList[event.oldIndex] = this.itemList[event.newIndex];
      this.itemList[event.newIndex] = temp;
    },
    /* call updateSequence api */
    async updateSequenceData(parent_stock_code:string) {
      // [origin sequence] : new sequence
      const sequence:{[key:number]:number|string} = {}
      for(let i=0; i<this.itemList.length;i++){
        console.log(this.itemList[i]);
        sequence[this.itemList[i].sequence] = i
      }
      const res = await updateChildStockCodeSequence(parent_stock_code, sequence);
      if (res.data.code==1){
          this.getChildStockItemsData(parent_stock_code)
        
      }
    },
    async getChildStockItemsData(parent_stock_code: string) {
      const res = await getChildStockItems(parent_stock_code);
      if (res.data) {
        console.log(res.data);
        this.itemList = res.data;
      }
    },
    async updateStockItemData(id:number,newItemForm: ItemForm) {
      const res = await patchStockItem(id,newItemForm);
      if (res.data) {
        return res.data;
      }
    },
    async createStockItemData(newItemForm: ItemForm) {
      newItemForm.stockCode = newItemForm.stockCode.toUpperCase()
      const res = await createStockItem(newItemForm);
      if (res.data.code==1) {
        this.itemList = [...this.itemList, {child: res.data, sequence:res.data.sequence}];
        return res.data;
      }else{
        return res.data
      }
    },
    async assignChildStockData(parent_stock_code: string, stock_code: string) {
      console.log(parent_stock_code, stock_code);

      const res = await assignChildStock(parent_stock_code, stock_code);
      if (res.data) {
        console.log(res.data);
        if(res.data.code==0) {
          return res.data
        }
        return res.data;
      }else{
        return res.data
      }
    },
    async removeStockItemChildData(
      parent_stock_code: string,
      stock_code: string,
      sequence: number
    ) {
      const res = await removeStockItemChild(parent_stock_code, stock_code, sequence);
      if (res.data) {
        this.getChildStockItemsData(parent_stock_code)
      //   const index = this.itemList.findIndex(
      //     (v) => v.child.sequence == sequence
      //   );
      //   this.itemList.splice(index, 1);
      //   return res.data;
      // }
      }
    },
  },
});

interface ItemList {
  child: StockItem;
  sequence: number
}

const itemList: ItemList[] = [];
